package Thread.Demo2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author wangzhipeng
 * @Date 2020-08-09 9:52 下午
 * @Description 多线程同时操作同一个对象
 */
public class TestThread4 implements Runnable{

    //票数
    private int ticketNums = 10;
    Lock lock = new ReentrantLock();

    public void run() {
        while(true){
            if(ticketNums <= 0)break;
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"张票");
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        TestThread4 testThread = new TestThread4();
        new Thread(testThread,"小明").start();
        new Thread(testThread,"老师").start();
        new Thread(testThread,"黄牛党").start();
    }
}
